package me.hl.guava.future;

import com.google.common.util.concurrent.*;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by pulangke on 2017/6/5.
 */
public class FutureTest {

    private Logger logger = LoggerFactory.getLogger(FutureTest.class);

    @Test
    public void test() {
        ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
        ListenableFuture future = service.submit(() -> {
            sleepQuietly(4);
        });

        Futures.addCallback(future, new FutureCallback() {
            @Override
            public void onSuccess(Object result) {
                logger.info("success");
            }

            @Override
            public void onFailure(Throwable t) {
                logger.info("fail");
            }
        });


        sleepQuietly(5);
        service.shutdown();
    }

    private void sleepQuietly(int second) {
        try {
            TimeUnit.SECONDS.sleep(second);
        } catch (InterruptedException e) {
        }
    }
}
